import { FormSchema } from '/@/components/Table';

import {
  getConstructionTeamApi,
  getContractorListApi,
  getContractorIdList,
  getConstructionTeamInfo,
} from '/@/api/zkrq/projectManagement';

let options = [];

export const formSchema = (deptId: string): FormSchema[] => [
  {
    slot: 'f3',
    field: 'f3',
    label: '',
    required: false,
    component: 'Input',
    colProps: {
      span: 24,
    },
  },

  {
    field: 'contractorId',
    component: 'ApiSelect',
    label: '项目承建商',
    required: true,
    colProps: {
      span: 12,
    },

    componentProps({ formActionType, formModel }) {
      return {
        allowClear: false,
        // api: getContractorInfoListApi,
        api: getContractorListApi,
        params: {
          // projectId: 1,
          keyWord: '',
        },
        resultField: 'records',
        labelField: 'value',
        valueField: 'key',
        // mode: 'multiple',
        getPopupContainer: () => document.body,
        async onChange(v) {
          // 承建商 getContractorIdList getConstructionTeamInfo
          const params = { contractorId: v };
          const res = await getContractorIdList(params);
          const {
            contractorIntroduction,
            contractorAddress,
            contractorHeadPhone,
            contractorHead,
            contractorName,
          } = res;
          console.log('%c====RES', 'color:#d12;font-size:30px;font-weight:bold', res);

          formModel.contractorIntroduction = contractorIntroduction;
          formModel.contractorAddress = contractorAddress;
          formModel.contractorHeadPhone = contractorHeadPhone;
          formModel.contractorHead = contractorHead;
          formModel.contractorName = contractorName;
          options = await getConstructionTeamApi({ contractorId: v, deptId: deptId });
          formActionType.setFieldsValue({
            constructionTeamId: '',
            constructionTeamPhone: '',
            constructionTeamHeadName: '',
            constructionTeamName: '',
          });
          formActionType.updateSchema({
            field: 'constructionTeamId',
            dynamicDisabled: false,
          });
        },
      };
    },
  },

  {
    componentProps: { placeholder: '暂无' },
    field: 'contractorName',
    label: '承建商名称',
    //required: true,
    dynamicDisabled: true,
    component: 'Input',
    colProps: {
      span: 12,
    },
  },

  {
    componentProps: { placeholder: '暂无' },
    dynamicDisabled: true,
    field: 'contractorHead',
    label: '承建负责人',
    // //required: true,
    component: 'Input',
    colProps: {
      span: 12,
    },
  },
  {
    componentProps: { placeholder: '暂无' },
    dynamicDisabled: true,
    field: 'contractorHeadPhone',
    label: '手机号码',
    //required: true,
    component: 'Input',
    colProps: {
      span: 12,
    },
  },

  {
    componentProps: { placeholder: '暂无' },
    dynamicDisabled: true,
    field: 'contractorAddress',
    label: '办公地址',
    //required: true,
    component: 'Input',
    colProps: {
      span: 24,
    },
  },

  {
    dynamicDisabled: true,
    field: 'contractorIntroduction',
    label: '承建商简介',
    //required: true,
    component: 'InputTextArea',
    colProps: {
      span: 24,
    },
    componentProps: {
      rows: 4,
      placeholder: '暂无',
    },
  },
  {
    slot: 'f4',
    field: 'f4',
    label: '',
    required: false,
    component: 'Input',
    colProps: {
      span: 24,
    },
  },

  {
    field: 'constructionTeamId',
    component: 'Select',
    label: '项目施工队',
    dynamicDisabled: true,
    required: true,
    colProps: {
      span: 12,
    },
    componentProps: ({ formModel }) => {
      return {
        // allowClear: true,
        // labelField: 'key',
        // valueField: 'value',
        // mode: 'multiple',
        fieldNames: {
          label: 'value',
          value: 'key',
        },
        options: options,
        getPopupContainer: () => document.body,
        onChange: async (v) => {
          if (!v) return;
          // 承建商 getContractorIdList getConstructionTeamInfo
          const params = { constructionTeamId: v };
          const res = await getConstructionTeamInfo(params);

          const { constructionTeamPhone, constructionTeamHeadName, constructionTeamName } = res;
          formModel.constructionTeamPhone = constructionTeamPhone;
          formModel.constructionTeamHeadName = constructionTeamHeadName;
          formModel.constructionTeamName = constructionTeamName;
        },
      };
    },
  },

  {
    componentProps: { placeholder: '暂无' },
    dynamicDisabled: true,
    field: 'constructionTeamName',
    label: '施工队名称',
    //required: true,
    component: 'Input',
    colProps: {
      span: 12,
    },
  },
  {
    componentProps: { placeholder: '暂无' },
    dynamicDisabled: true,
    field: 'constructionTeamHeadName',
    label: '施工队长',
    //required: true,
    component: 'Input',
    colProps: {
      span: 12,
    },
  },
  {
    componentProps: { placeholder: '暂无' },
    dynamicDisabled: true,
    field: 'constructionTeamPhone',
    label: '手机号码',
    //required: true,
    component: 'Input',
    colProps: {
      span: 12,
    },
  },
  // {
  //   field: 'belongModuleId',
  //   component: 'ApiSelect',
  //   label: '所属模块',
  //   colProps: {
  //     span: 12,
  //   },
  //   componentProps: {
  //     api: getModuleFirstListApi,
  //     labelField: 'meta.title',
  //     valueField: 'meta.moduleId',
  //     getPopupContainer: () => document.body,
  //   },
  // },
  // {
  //   field: 'userList',
  //   component: 'ApiSelect',
  //   label: '包含用户',
  //   colProps: {
  //     span: 24,
  //   },
  //   componentProps: {
  //     api: getUserListByPage,
  //     resultField: 'records',
  //     labelField: 'username',
  //     valueField: 'userId',
  //     mode: 'multiple',
  //     getPopupContainer: () => document.body,
  //   },
  // },
  // {
  //   field: 'status',
  //   label: '状态',
  //   component: 'RadioButtonGroup',
  //   defaultValue: '0',
  //   componentProps: {
  //     options: [
  //       { label: '启用', value: '0' },
  //       { label: '停用', value: '1' },
  //     ],
  //   },
  // },
  // {
  //   label: '备注',
  //   field: 'roleDesc',
  //   component: 'InputTextArea',
  //   colProps: {
  //     span: 24,
  //   },
  // },
];
